<template>
  <div class="login" @keyup.enter="submit">
    <h1 style="text-align: center;margin-bottom: 50px">管理后台</h1>
    <el-row>
      <el-col :span="6" :offset="9">
        <el-form :model="user" ref="loginForm">
          <el-form-item prop="name" :rules="mandatory" ref="nameField">
            <el-input v-model="user.name" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password" :rules="mandatory">
            <el-input type="password" v-model="user.password" placeholder="密码" >
            </el-input>
          </el-form-item>
          <el-form-item prop="captcha" :rules="mandatory" ref="captchaField">
            <div style="white-space: nowrap" class="asd">
              <el-row>
                <el-col :span="7" >
                  <el-input v-model="user.captcha"/>
                </el-col>
                <el-col :span="7">
                  <img :src="captchaUrl" style="vertical-align: middle;width: 100%;" />
                </el-col>
                <el-col :span="10">
                  <el-button native-type="button" @click="timestamp = Date.now()">重新获取</el-button>
                </el-col>
              </el-row>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit" :loading="loading">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-alert
      style="position: fixed; top: 70px; z-index: 9999;"
      :title="error || ''"
      type="error"
      show-icon
      center
      v-show="error"
      @close="clearError">
    </el-alert>
    <el-alert
      style="position: fixed; top: 70px; z-index: 9999;"
      :title="info || ''"
      type="success"
      show-icon
      center
      v-show="info"
      @close="clearInfo">
    </el-alert>
  </div>
</template>

<script>
  import ApiClient from '@/utils/ApiClient'
  import {setStore} from '@/utils/storage'
  import {mapGetters, mapMutations} from 'vuex'


  export default {
    data() {
      return {
        user: {
          name: null,
          password: null,
          captcha: null
        },
        timestamp: Date.now(),
        mandatory: [
          {required: true, message: '不能为空'}
        ],
        loading:false,
      }
    },

    computed: {
      captchaUrl() {
        return `${process.env.API_URL_BASE}/commons/icaptcha/getCaptcha.do?${this.timestamp}`
      },
      ...mapGetters(['info', 'error'])
    },
    methods: {
      submit() {
        //按钮显示登录状态
        this.$refs.loginForm.validate(valid => {
          if (!valid) return;
          this.loading = true;
          ApiClient.post('/backend/users/login.do', this.user, {silent: false}).then((user) => {
            setStore('user_museum', JSON.stringify(user));
            this.$router.push('/exhibit/exhibitList');
            this.loading = false;
          }).catch(message => {
            this.loading = false;
          })
        });
      },
      ...mapMutations(['clearInfo', 'clearError'])
    }
  }
</script>
<style>
  .login .el-form-item__content button.el-button--primary{
    width:100%;
  }
  .login .asd button.el-button{
    font-size: 12px;
    width:100%;
    overflow: hidden;
    line-height: 14px;
  }
</style>
